package com.example.timeemail.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.timeemail.R;
import com.example.timeemail.entity.Person;

import java.util.ArrayList;
import java.util.List;

public class PersonAdapter extends BaseAdapter {

    private List<Person> pdata = new ArrayList<Person>();

    private Context context;

    public PersonAdapter(List<Person> pdata, Context context) {
        this.pdata = pdata;
        this.context = context;
    }

    @Override
    public int getCount() {
        return pdata.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //优化ListView
    //定义一个ViewHolder静态类
    static class ViewHolder{
        //定义属性，对应是列表项数据
        public ImageView myimg;
        public TextView myname;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //定义ViewHolder对象
        ViewHolder holder;
        //判断convertView是否为空，convertView对应的列表项
        if (convertView==null){
            //新建
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.driftitem,parent,false);
            holder.myimg = (ImageView) convertView.findViewById(R.id.item_img);
            holder.myname = (TextView) convertView.findViewById(R.id.item_name);

            convertView.setTag(holder);

        }else{
            //复用列表项
            holder = (ViewHolder) convertView.getTag();

        }
        //设置列表项数据
        holder.myimg.setImageResource(pdata.get(position).getImg());
        holder.myname.setText(pdata.get(position).getName());

        return convertView;
    }
}
